/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import java.io.IOException;

import org.aroundme.controller.ApplicationController;
import org.aroundme.controller.SearchController;
import org.aroundme.model.Place;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.unitTools.UniqueIdGenerator;
import org.springframework.mock.web.MockHttpServletResponse;

public class SearchControllerTest extends TestFixture {
	private SearchController searchController;
	private MockHttpServletResponse mockResponse;
	private ApplicationController appController;
	
	protected void setUp() throws Exception {
		super.setUp();
		searchController = (SearchController)springFactory.getBean(SearchController.class.getSimpleName());
		appController = (ApplicationController)springFactory.getBean(ApplicationController.class.getSimpleName());
		mockResponse = new MockHttpServletResponse();
		appController.setResponse(mockResponse);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		searchController = null;
		appController = null;
		mockResponse = null;
	}

	public void testSearchAND() throws IOException {
		Place p = xmlTestUtils.getDummyPlaces().get(2);
		Place pNotPresent = xmlTestUtils.getDummyPlaces().get(0);

		String key1 = "" + new UniqueIdGenerator().getUniqueId();
		String key2 = "" + new UniqueIdGenerator().getUniqueId();
		
		p.setName("This is a test " + key1 + "name... " + key2 + " test");
		p.setLocation(xmlTestUtils.getDummyLocations().get(1));
		
		pNotPresent.setName("Hi,this - " + key1 + " - place shouldn't be there, " + key2 + " plus some other...");
		pNotPresent.setLocation(xmlTestUtils.getDummyLocations().get(2));
		
		xmlTestUtils.saveDummyLocation(xmlTestUtils.getDummyLocations().get(1));
		xmlTestUtils.saveDummyLocation(xmlTestUtils.getDummyLocations().get(2));
		xmlTestUtils.saveDummyUsersToDB();
		xmlTestUtils.saveDummyPlacesToDB();
		
		searchController.search(key1 + " " + key2, 0, 99999, xmlTestUtils.getDummyLocations().get(1).getCellid());
		
		String response = mockResponse.getContentAsString();
		
		assertTrue("id of place 1 should be in XML string", response.contains("<id>" + p.getId() + "</id>"));
		assertTrue("title should be in XML string", response.contains("<name>" + p.getName() + "</name>"));
		assertFalse("id of place 2 shouldn't be there", response.contains("<id>" + pNotPresent.getId() + "</id>"));
	}
	
	public void testSearchOR() throws IOException{
		String key1 = "" + new UniqueIdGenerator().getUniqueId();
		String key2 = "" + new UniqueIdGenerator().getUniqueId();
		String key3 = "" + new UniqueIdGenerator().getUniqueId();
		
		Place p1 = xmlTestUtils.getDummyPlaces().get(1);
		p1.setName("This is a test " + key1 + "name... " + key2 + " test");
		p1.setLocation(xmlTestUtils.getDummyLocations().get(1));
		
		Place p2 = xmlTestUtils.getDummyPlaces().get(2);
		p2.setName("Hey, did you see " + key2 + " and " + key3 + " in ...?");
		p2.setLocation(xmlTestUtils.getDummyLocations().get(1));
		Place p3 = xmlTestUtils.getDummyPlaces().get(0);
		p3.setName("Hey, " + key2 + ", and " + key3 + " in the");
		p3.setLocation(xmlTestUtils.getDummyLocations().get(2));
		
		xmlTestUtils.saveDummyLocation(xmlTestUtils.getDummyLocations().get(1));
		xmlTestUtils.saveDummyLocation(xmlTestUtils.getDummyLocations().get(2));
		xmlTestUtils.saveDummyUsersToDB();
		xmlTestUtils.saveDummyPlacesToDB();
		
		searchController.search(key1 + " OR " + key2 + " or " + key3, 0, 99999, xmlTestUtils.getDummyLocations().get(1).getCellid());
		
		String response = mockResponse.getContentAsString();
		
		assertTrue("id of p1 should be in XML", response.contains("<id>" + p1.getId() + "</id>"));
		assertTrue("id of p2 should be in XML", response.contains("<id>" + p2.getId() + "</id>"));
		assertTrue("name of p1 should be in XML string", response.contains("<name>" + p1.getName() + "</name>"));
		assertTrue("name of p2 should be in XML string", response.contains("<name>" + p2.getName() + "</name>"));
		assertFalse("id of p3 shouldn't be in XML string", response.contains("<id>" + p3.getId() + "</id>"));
	}

}
